# -*- coding: UTF-8 -*-
# version 1.0
# Ip子网 Server层
from mxsoftpy import Server
from db.common.NetworkManage.IpAM.IpSubnet import IpSubnetDB
from model.common.NetworkManage.IpAM.IpSubnet import IpSubnetModel
from mxsoftpy.exception import DataError
from index import _


class IpSubnetServer(Server):
    __model__ = IpSubnetModel
    __db__ = IpSubnetDB

    def create(self, *args, **kwargs):
        """
        添加IP子网数据
        :param kwargs: 数据字典
        """
        return self.db.create(**kwargs)

    def delete(self, *args, **kwargs):
        """
        删除IP子网
        :param kwargs: 数据字典
        """
        return self.db.delete(**kwargs)

    def ignore(self, **kwargs):
        """
        忽略IP子网
        :param kwargs: 数据字典
        :return:
        """
        return self.db.ignore(**kwargs)

    def retrieve(self, **kwargs):
        """
        回显数据
        :param kwargs: 数据字典
        :return:
        """
        return self.db.retrieve(**kwargs)

    def IpCount(self, **kwargs):
        """
        统计子网数量和在用IP
        :param kwargs: 数据字典
        :return:
        """
        SubnetCount_int, IpCount_int = self.db.IpCount(**kwargs)
        return {'SubnetCount': SubnetCount_int, 'IpCount': IpCount_int}

    def SubnetRecovery(self, **kwargs):
        """
        恢复子网
        :param kwargs: 数据字典
        :return:
        """
        return self.db.SubnetRecovery(**kwargs)

    def ScanningSet(self, **kwargs):
        """
        自动扫描操作
        :param kwargs:
        :return:
        """
        if 'type' not in kwargs:
            raise DataError(_('类型不能为空'))
        # 自动扫描设置获取
        if kwargs['type'] == '1':
            return self.db.GetScanning(**kwargs)
        # 自动扫描设置修改
        elif kwargs['type'] == '0':
            return self.db.ScanningAdd(**kwargs)
        else:
            raise DataError(_('类型错误'))

    def Scanning(self, **kwargs):
        """
        扫描
        :param kwargs: 数据字典
        :return:
        """
        if 'type' not in kwargs:
            return DataError(_('类型不能为空'))
        self.db.scanning()
        # 网络
        if kwargs['type'] == '0':
            return self.db.GetNetwork(**kwargs)
        # 组
        elif kwargs['type'] == '1':
            return self.db.GetGroupInfo(**kwargs)
        # 子网
        elif kwargs['type'] == '2':
            return self.db.GetSubnetInfo(**kwargs)
        return self.db.Scanning(**kwargs)

    def FindIp(self, **kwargs):
        """
        发现IP
        :param kwargs: 数据字典
        :return:
        """
        return self.db.FindIp(**kwargs)
